;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var router = require("../hash/pageRouter");
	var databind = require("databind");
	var css_riskWarningScheme = require("./warningSchemeResultList.css");
	var html_settlement = require("../exception/html_settlement.html");
	var settled;
	var oData;
	var tools = require("tools");
	var init = function(page) {
		oData ={
			pageIndex: 1,
			pageSize: 10, 
			totalPageCount: 1, 
			totalRecordCount: 0,
			factor: 0
		}
		
		if (page.params != null) {
			settled = page.params.settled;
		}
		
		router.init();
		initEvent();
	};
	
	var initEvent = function() {
		// 若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == ""|| base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		
		if (settled == null) {
			oData.settlementState = 0;
		} else {
			oData.settlementState = 1;
		}
		
		//excel导出
		$("#warningSchemeResultList .export").unbind("click").click(function(){
			var settlementState = oData.settlementState;
			var financeContractSerialNumber = oData.financeContractSerialNumber;
			var financeNumber = oData.financeNumber;
			var enterpriseName = encodeURI(oData.enterpriseName);
			var productType = oData.productType;
			var factor = oData.factor;
			var href = "/finance/api/v1.0/ExportSchemeResultInfo.json?";
			href = href + "settlementState=" + settlementState + "&";
			href = href + "financeContractSerialNumber=" + financeContractSerialNumber + "&";
			href = href + "financeNumber=" + financeNumber + "&";
			href = href + "enterpriseName=" + enterpriseName + "&";
			href = href + "productType=" + productType + "&";
			href = href + "factor=" + factor;
			
			window.open(href);
		});
		
		$("#warningSchemeResultList .search").unbind("click").click(function(){
			oData.enterpriseName = $("#warningSchemeResultList input[name=enterpriseName]").val();
			oData.financeNumber = $("#warningSchemeResultList input[name=financeNumber]").val();
			oData.financeContractSerialNumber = $("#warningSchemeResultList input[name=financeContractSerialNumber]").val();
			oData.productType = $("#warningSchemeResultList select[name=productType]").val();
			oData.pageIndex = 1;
		
			tableInit();
			tools.loadList("inquireWarningSchemeResultPageList.json", $("#warningSchemeResultTable tbody"), $("#warningSchemeResultTpl"), oData, function(){
				$("#warningSchemeResultTable").trigger("update");
				$(window).resize();
				
				//详情
				$("#warningSchemeResultList .detail").unbind("click").click(function(){
					router.route("auditWarning/warningSchemeResultDetail.html?schemeResultId=" + $(this).parents("tr").attr("schemeResultId"));
				});
				
				if (settled == "true") {
					$("#warningSchemeResultList .schemeDeal").remove();
				}
				
				//预警处理
				$(".schemeDeal").unbind("click").click(function() {
					$("#popdialog").html(html_settlement);
					$("#settlement_risk_dialog").attr("action", "settlementSchemeResult");
					$("#settlement_risk_dialog .dialogTitle").html("预警处理");
					$("#settlement_risk_dialog input[name=abnormalRiskId]").attr("name", "schemeResultId");
					dialog.showDialog($("#settlement_risk_dialog"));
					$("#settlement_risk_dialog input[name=schemeResultId]").val($(this).parents("tr").attr("schemeResultId"));
					$("#settlement_risk_dialog .sure_yes").unbind("click").click(function() {
						databind.submitDom($("#settlement_risk_dialog"), function(){
							base.alert(1, "处理成功！");
							dialog.hideDialog($("#settlement_risk_dialog"));
							router.route("auditWarning/warningSchemeResultList.html");
						});
					});
				});
			}, $("#warningSchemeResultList .pagerCon"));
		
		});
		
		
		tableInit();
		tools.loadList("inquireWarningSchemeResultPageList.json", $("#warningSchemeResultTable tbody"), $("#warningSchemeResultTpl"), oData, function(){
			$("#warningSchemeResultTable").trigger("update");
			$(window).resize();
			
			//详情
			$("#warningSchemeResultList .detail").unbind("click").click(function(){
				router.route("auditWarning/warningSchemeResultDetail.html?schemeResultId=" + $(this).parents("tr").attr("schemeResultId"));
			});
			
			if (settled == "true") {
				$("#warningSchemeResultList .schemeDeal").remove();
			}
			
			//预警处理
			$(".schemeDeal").unbind("click").click(function() {
				$("#popdialog").html(html_settlement);
				$("#settlement_risk_dialog").attr("action", "settlementSchemeResult");
				$("#settlement_risk_dialog .dialogTitle").html("预警处理");
				$("#settlement_risk_dialog input[name=abnormalRiskId]").attr("name", "schemeResultId");
				dialog.showDialog($("#settlement_risk_dialog"));
				$("#settlement_risk_dialog input[name=schemeResultId]").val($(this).parents("tr").attr("schemeResultId"));
				$("#settlement_risk_dialog .sure_yes").unbind("click").click(function() {
					databind.submitDom($("#settlement_risk_dialog"), function(){
						base.alert(1, "处理成功！");
						dialog.hideDialog($("#settlement_risk_dialog"));
						router.route("auditWarning/warningSchemeResultList.html");
					});
				});
			});
		}, $("#warningSchemeResultList .pagerCon"));
	};
	
	//tablesorter初始化
	var tableInit = function() {
		  $("#warningSchemeResultTable").tablesorter({
		    widthFixed: false,
			theme: 'blue',
			showProcessing: false,
			 tabIndex: true,
			 sortMultiSortKey: "shiftKey",
		    sortResetKey: 'ctrlKey',
			ignoreCase: true,
			sortForce: null,
			resort:false,
			headers:{
				 0: {sorter: false},
				 1: {sorter: false},
      			 2: {sorter: false},
      			 3: {sorter: false},
      			 4: {sorter: false},
      			 5: {sorter: false},
      			 6: {sorter: false},
      			 7: {sorter: false},
      			 8: {sorter: false},
      			 9: {sorter: false},
      			 10: {sorter: false},
      			 11: {sorter: false},
      			 12: {sorter: false},
      			 13: {sorter: false},
      			 14: {sorter: false},
      			 15: {sorter: false},
      			 16: {sorter: false},
      			 17: {sorter: false},
      			 18: {sorter: false},
      			 19: {sorter: false},
      			 20: {sorter: false},
      			 21: {sorter: false},
      			 22: {sorter: false}
				},
			headerTemplate : '{content} {icon}',
			widgets: [ 'zebra', 'scroller' ],
			widgetOptions : {
		      scroller_height : 420,
		      // scroll tbody to top after sorting
		      scroller_upAfterSort: true,
		      // pop table header into view while scrolling up the page
		      scroller_jumpToHeader: true,
		      // In tablesorter v2.19.0 the scroll bar width is auto-detected
		      // add a value here to override the auto-detected setting
		      scroller_barWidth : null
		      // scroll_idPrefix was removed in v2.18.0
		      // scroller_idPrefix : 's_'
		    }
		  });
		  
			//事件
		  	$("#warningSchemeResultTable").bind("sortEnd",function(event) {
				var table = event.target;
				var columnSort       =  table.config.sortList[0][0];          									 //第几列排序，序号
				var columnSortName   =  $(table.config.headerList[table.config.sortList[0][0]]).text();          //第几列排序，名称
				var columnSortType   =  table.config.sortList[0][1];    	  									 //0:升序，1：降序
				//测试代码2：排序
			});
	};
	
	return {
		init : init
	};
});